package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gcgl.domain.GcWddqtx;
import com.ruoyi.gcgl.service.IGcWddqtxService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.ReturnUUID;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文档到期提醒Controller
 * 
 * @author ruoyi
 * @date 2023-04-28
 */
@Controller
@RequestMapping("/gcgl/wddqtx")
public class GcWddqtxController extends BaseController
{
    private String prefix = "gcgl/wddqtx";

    @Autowired
    private IGcWddqtxService gcWddqtxService;

    @RequiresPermissions("gcgl:wddqtx:view")
    @GetMapping()
    public String wddqtx()
    {
        return prefix + "/wddqtx";
    }

    /**
     * 查询文档到期提醒列表
     */
    @RequiresPermissions("gcgl:wddqtx:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcWddqtx gcWddqtx)
    {
        startPage();
        gcWddqtx.setDeleteFlag(1);
        List<GcWddqtx> list = gcWddqtxService.selectGcWddqtxList(gcWddqtx);
        for(GcWddqtx obj:list){
        	if(obj.getZlwj()!=null && !obj.getZlwj().equals("")){
        	obj.setZlwj(RuoYiConfig.getOnPath()+obj.getZlwj());
        	}
        }
        return getDataTable(list);
    }

    /**
     * 导出文档到期提醒列表
     */
    @RequiresPermissions("gcgl:wddqtx:export")
    @Log(title = "文档到期提醒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcWddqtx gcWddqtx)
    {
        List<GcWddqtx> list = gcWddqtxService.selectGcWddqtxList(gcWddqtx);
        ExcelUtil<GcWddqtx> util = new ExcelUtil<GcWddqtx>(GcWddqtx.class);
        return util.exportExcel(list, "文档到期提醒数据");
    }

    /**
     * 新增文档到期提醒
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	mmap.put("code", ReturnUUID.getOrderNo("WD"));
        return prefix + "/add";
    }

    /**
     * 新增保存文档到期提醒
     */
    @RequiresPermissions("gcgl:wddqtx:add")
    @Log(title = "文档到期提醒", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcWddqtx gcWddqtx,@RequestParam("zilwjfile")MultipartFile filewj){
    	
    	try {
    		if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcWddqtx.setZlwj(zilwj);
            }
    		gcWddqtx.setStatus(0);
            gcWddqtx.setDeleteFlag(1);
            gcWddqtx.setCreateAt(new Date());
            gcWddqtx.setUpdateAt(new Date());
            gcWddqtx.setCreateUser(getLoginName());
            gcWddqtx.setUpdateUser(getLoginName());
            gcWddqtxService.insertGcWddqtx(gcWddqtx);
    		
    		
		} catch (Exception e) {
			// TODO: handle exception
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 修改文档到期提醒
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcWddqtx obj = gcWddqtxService.selectGcWddqtxById(id);
        if(obj.getZlwj()!=null && !obj.getZlwj().equals("")){
        	obj.setZlwj(RuoYiConfig.getOnPath()+obj.getZlwj());
        }
        mmap.put("gcWddqtx", obj);
        mmap.put("downloadZilwj", obj.getZlwj());
        return prefix + "/edit";
    }

    /**
     * 修改保存文档到期提醒
     */
    @Log(title = "文档到期提醒", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcWddqtx gcWddqtx,@RequestParam("zilwjfile")MultipartFile filewj)
    {
    	try {
    		if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcWddqtx.setZlwj(zilwj);
            }
    		gcWddqtxService.updateGcWddqtx(gcWddqtx);
		} catch (Exception e) {
			// TODO: handle exception
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 删除文档到期提醒
     */
    @RequiresPermissions("gcgl:wddqtx:remove")
    @Log(title = "文档到期提醒", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcWddqtxService.deleteGcWddqtxByIds(ids));
    }
}
